package com.med.meddataintegrator.patient.service.Impl;

import com.med.meddataintegrator.common.basic.response.JsonResult;
import com.med.meddataintegrator.common.mapper.*;
import com.med.meddataintegrator.common.pojo.dos.AppointmentDo;
import com.med.meddataintegrator.common.pojo.dos.DrugsDo;
import com.med.meddataintegrator.common.pojo.dos.HospitalizationDo;
import com.med.meddataintegrator.common.pojo.dos.PatientDo;
import com.med.meddataintegrator.common.pojo.dto.patinet.PatientQueryParams;

import com.med.meddataintegrator.common.pojo.vo.appointment.AppointmentVo;
import com.med.meddataintegrator.common.pojo.vo.doctor.DoctorVo;
import com.med.meddataintegrator.common.pojo.vo.hospitalization.HospitalizationVo;
import com.med.meddataintegrator.common.pojo.vo.patient.PatientVo;
import com.med.meddataintegrator.patient.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PatientImpl implements PatientService {

    @Autowired
    private PatientDoMapper patientDoMapper;
    @Autowired
    private DrugsDoMapper drugsDoMapper;
    @Autowired
    private AppointmentDoMapper appointmentDoMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private HospitalizationDoMapper hospitalizationDoMapper;


    @Override
    public JsonResult getlist(PatientQueryParams params) {


        List<PatientVo> patientVos = new ArrayList<>();
        System.out.println(params.toString());

        List<PatientDo> lists =  patientDoMapper.selectByParams(params);

        // 预约信息的合并
        List<PatientVo> collect = lists.stream().filter(patientDo -> patientDo.getAppointmentId() != null).map(
                patientDo -> {
                    // 进行预约信息的合并
                    Integer appointmentId = patientDo.getAppointmentId();
                    AppointmentDo appointmentDo = appointmentDoMapper.selectByPrimaryKey(Long.valueOf(appointmentId));
                    //其中存在医生id 以及  患者id; 此处需要替换为姓名
                    Integer patientId = appointmentDo.getPatientId();
                    PatientDo patient = patientDoMapper.selectByPrimaryKey(Long.valueOf(patientId));
                    Integer doctorId = appointmentDo.getDoctorId();
                    DoctorVo doctorVo = doctorMapper.selectByIdDoctor(Long.valueOf(doctorId));
                    AppointmentVo appointmentVo = AppointmentVo.builder()
                            .id(appointmentDo.getId())
                            .patientName(patient.getName())
                            .doctorName(doctorVo.getName())
                            .time(appointmentDo.getTime())
                            .expenses(appointmentDo.getExpenses())
                            .status(appointmentDo.getStatus())
                            .build();

                    PatientVo patientVo = PatientVo.builder()
                            .id(patientDo.getId())
                            .name(patientDo.getName())
                            .age(patientDo.getAge())
                            .certId(patientDo.getCertId())
                            .sex(patientDo.getSex())
                            .address(patientDo.getAddress())
                            .appointmentVo(appointmentVo)
                            .build();

                    // 进行药品信息的合并 @作为分隔符
                    String drugsids = patientDo.getDrugsids();
                    String drugsName = "";
                    if (drugsids != null && !drugsids.isEmpty()) {
                        String[] split = drugsids.split("@");
                        for (String s : split) {
                            Integer id = Integer.valueOf(s);
                            DrugsDo drugsDo = drugsDoMapper.selectByPrimaryKey(Long.valueOf(id));
                            if (drugsDo != null){
                                drugsName += drugsDo.getName() + " ";
                            }
                        }
                        patientVo.setDrugs(drugsName);



                        // 住院信息详情合并
                        Integer hospitalizationId = patientDo.getHospitalizationId();
                        if (hospitalizationId != null){
                            HospitalizationDo hospitalizationDo = hospitalizationDoMapper.selectByPrimaryKey(Long.valueOf(hospitalizationId));
                            HospitalizationVo hospitalizationVo = HospitalizationVo.builder()
                                    .id(hospitalizationDo.getId())
                                    .floor(hospitalizationDo.getFloor())
                                    .bed(hospitalizationDo.getBed())
                                    .door(hospitalizationDo.getDoor())
                                    .medicalName(hospitalizationDo.getMedicalName())
                                    .intime(hospitalizationDo.getIntime())
                                    .outtime(hospitalizationDo.getOuttime())
                                    .build();
                            patientVo.setHospitalizationVo(hospitalizationVo);
                        }


                    }
                    patientVos.add(patientVo);
                    return patientVo;
                }
        ).collect(Collectors.toList());

        return JsonResult.ok(collect);
    }



}
